package com.okyai.kt.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.okyai.common.annotation.Log;
import com.okyai.common.enums.BusinessType;
import com.okyai.kt.domain.Topic;
import com.okyai.kt.domain.TopicItem;
import com.okyai.kt.domain.TopicPlan;
import com.okyai.kt.service.ITopicItemService;
import com.okyai.kt.service.ITopicPlanService;
import com.okyai.kt.service.ITopicService;
import com.okyai.common.core.controller.BaseController;
import com.okyai.common.core.domain.AjaxResult;
import com.okyai.common.utils.poi.ExcelUtil;
import com.okyai.common.core.page.TableDataInfo;

/**
 * 课题Controller
 * 
 * @author okyai
 * @date 2021-03-20
 */
@Controller
@RequestMapping("/kt/topic")
public class TopicController extends BaseController
{
    private String prefix = "kt/topic";

    @Autowired
    private ITopicService topicService;
    
    @Autowired
    private ITopicPlanService topicPlanService;
    
    @Autowired
    private ITopicItemService topicItemService;

    @RequiresPermissions("kt:topic:view")
    @GetMapping()
    public String topic()
    {
        return prefix + "/topic";
    }

    /**
     * 查询课题列表
     */
    @RequiresPermissions("kt:topic:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Topic topic)
    {
        startPage();
        List<Topic> list = topicService.selectTopicList(topic);
        return getDataTable(list);
    }

    /**
     * 导出课题列表
     */
    @RequiresPermissions("kt:topic:export")
    @Log(title = "课题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Topic topic)
    {
        List<Topic> list = topicService.selectTopicList(topic);
        ExcelUtil<Topic> util = new ExcelUtil<Topic>(Topic.class);
        return util.exportExcel(list, "topic");
    }

    /**
     * 新增课题
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存课题
     */
    @RequiresPermissions("kt:topic:add")
    @Log(title = "课题", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Topic topic)
    {
        return toAjax(topicService.insertTopic(topic));
    }

    /**
     * 修改课题
     */
    @GetMapping("/edit/{topicId}")
    public String edit(@PathVariable("topicId") Long topicId, ModelMap mmap)
    {
        Topic topic = topicService.selectTopicById(topicId);
        mmap.put("topic", topic);
        return prefix + "/edit";
    }

    /**
     * 修改保存课题
     */
    @RequiresPermissions("kt:topic:edit")
    @Log(title = "课题", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Topic topic)
    {
        return toAjax(topicService.updateTopic(topic));
    }

    /**
     * 删除课题
     */
    @RequiresPermissions("kt:topic:remove")
    @Log(title = "课题", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(topicService.deleteTopicByIds(ids));
    }
    
    
    @RequiresPermissions("kt:topic:view")
    @GetMapping("/item/{topicId}")
    public String item(@PathVariable("topicId") Long topicId,ModelMap mmap)
    {
    	mmap.put("topic", topicService.selectTopicById(topicId));
        return prefix + "/item/item";
    }
    
    @GetMapping("/selectItem/{itemType}")
    public String selectItem(@PathVariable("itemType") String itemType) {
    	String url= "";
    	if(("W").equals(itemType)){
    		url = prefix+"/select/questionnaire";
    	}else {
    		url = prefix+"";
    	}
    	return url;
    }
    
    @GetMapping("/configPlan/{topicId}")
    public String configPlan(@PathVariable("topicId") Long topicId,ModelMap mmap) {
    	TopicPlan plan = topicPlanService.selectTopicPlanById(topicId);
    	//先查詢plan
    	mmap.put("plan", plan);
    	//在查詢項目LIST
    	TopicItem item = new TopicItem();
    	item.setTopicId(plan.getTopicId());
    	mmap.put("itemList", topicItemService.selectTopicItemList(item));
    	return prefix + "/configPlan";
    }
    
}
